﻿using System;
using System.Data;

namespace ExtensionClassLibrary
{
    #region DBCommand
    /// <summary>
    /// 用于集合sql或表table
    /// </summary>
    public class DBTable
    {
        /// <summary>
        /// 表table操作
        /// 1: 新增, 2:新增or更新, 3:sql语句（简单sql、删除）
        /// </summary>
        public int Type { get; set; }
        /// <summary>
        /// sql语句
        /// </summary>
        public string SqlStr { get; set; }
        /// <summary>
        /// 主键
        /// </summary>
        public string Keys { get; set; }
        /// <summary>
        /// 更新列
        /// </summary>
        public string ColUpdate { get; set; }
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName { get; set; }
        /// <summary>
        /// 表
        /// </summary>
        public DataTable Dt { get; set; }
        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="dBEntity"></param>
        public void InitData(IDBEntity dBEntity, int serverno)
        {
            if (dBEntity == null)
            {
                return;
            }
            this.TableName = dBEntity.GetTableName();
            this.Dt = dBEntity.GetTable();
            this.Keys = dBEntity.GetKeys();
            this.ColUpdate = dBEntity.GetAllUpdateCols();
            this.SqlStr = dBEntity.GetSqlDelete(serverno);
        }
    }
    public class DBCommand
    {
        public int ServiceNo { get; set; }
        public object CommandData { get; set; }
        public IDBEntity DBEntity { get; set; }
        /// <summary>
        /// sql语句及表相关数据
        /// </summary>
        public DBTable DbTable { get; set; } = new DBTable();
        /// <summary>
        /// 记数
        /// </summary>
        public Int64 Num { get; set; } = -1;
        /// <summary>
        /// 处理类型：0本地处理，1持久化处理
        /// </summary>
        public DBCommandType Type { get; set; } = DBCommandType.local;
        /// <summary>
        /// 用于，DB保存成功后，写计数文件
        /// </summary>
        public string SuccessDBlogFilePath
        {
            get
            {
                string result = "DBlogSuccessFlagLocal";
                switch (Type)
                {
                    case DBCommandType.Persistent:
                        return "DBlogSuccessFlagPersistent";
                        break;
                    default:
                        return result;
                        break;
                }
                return result;
            }
        }
        /// <summary>
        /// 用于，DB恢复文件
        /// </summary>
        public string RecoverDataFilePath
        {
            get
            {
                string result = "RecoverData";
                switch (Type)
                {
                    case DBCommandType.Persistent:
                        return "RecoverDataPersistent";
                        break;
                    default:
                        return result;
                        break;
                }
                return result;
            }
        }
    }
    public enum DBCommandType
    {
        /// <summary>
        /// 0本地处理
        /// </summary>
        local = 0,
        /// <summary>
        /// 1持久化处理
        /// </summary>
        Persistent = 1
    }
    /// <summary>
    /// 记数类型
    /// </summary>
    public enum DBCommandNumType
    {
        /// <summary>
        /// 0第一行记数
        /// </summary>
        before = 0,
        /// <summary>
        /// 1最后一行记数
        /// </summary>
        end = 1
    }
    /// <summary>
    /// 主要用于sqlbulkcopy数据库表对表映射处理
    /// </summary>
    public interface IDBEntity
    {
        /// <summary>
        /// 获取表名
        /// </summary>
        /// <returns></returns>
        string GetTableName();
        /// <summary>
        /// 获取主键
        /// </summary>
        /// <returns></returns>
        string GetKeys();
        /// <summary>
        /// 获取更新列
        /// </summary>
        /// <returns></returns>
        string GetAllUpdateCols();
        /// <summary>
        /// 获取所有列
        /// </summary>
        /// <returns></returns>
        string GetAllCols();
        /// <summary>
        /// 获取当前表结构
        /// </summary>
        /// <returns></returns>
        DataTable GetTable();
        /// <summary>
        /// 获取删除sql
        /// </summary>
        /// <param name="ServiceNo"></param>
        /// <returns></returns>
        string GetSqlDelete(int ServiceNo);

    }
    #endregion
}
